<?php
class SearchController extends Zend_Controller_Action
{
    public function indexAction ()
    {}
    public function resultAction ()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $result = null;
        $output = '';
        $params = array_diff($request->getParams(), $request->getUserParams());
        $criteria = array();
        foreach ($params as $colName => $value) {
            if (is_array($value)) {
                foreach ($value as $key => $arrayVal) {
                    $value[$key] = htmlentities(trim($arrayVal));
                }
            } else {
                $value = htmlentities(trim($value));
            }
            $criteria[$colName] = $value;
        }
        if ('project' == strtolower($criteria['estate_type'])) {
            $basicCriteria = array('estate_id', 'state', 'district', 'city', 
            'is_featured', 'estate_type', 'name');
            $specCriteria = array('type_id', 'category_id', 'bhk', 
            'rate_per_unit', 'estate_for', 'size', 'sizeRangeFactor', 'price', 
            'priceRangeFactor');
            $basicParams = array_intersect_key($criteria, 
            array_flip($basicCriteria));
            $specParams = array_intersect_key($criteria, 
            array_flip($specCriteria));
            $projectMapper = new Reale_Model_Estate_ProjectMapper();
            $result = $projectMapper->searchByParams($basicParams, $specParams);
            
        
        if (count($result)) {
            foreach ($result as $estate_id => $estateArray) {
                echo '<div class="estateSet">';
                echo '<div class="estateInfo">';
                $url = $this->_helper->url('index', 'projectprofile', 'default', 
                array('project_id' => $estate_id));
                $featured = $estateArray[0]['is_featured'] ? '*' : '';
                echo '<a class="resultTitle" href="' . $url .
                 '" target="_blank" >' . $estateArray[0]['project_name'] . ', ' .
                 $estateArray[0]['district'] . ', ' . $estateArray[0]['state'] .
                 '</a>' . $featured;
                echo '<p class="resultBrief">';
                if (count($estateArray) > 1) {
                    echo 'There are ' . count($estateArray) . ' units in ' .
                     $estateArray[0]['project_name'] . ' matching your criteria.';
                } else {
                    $first = false;
                    if (isset($estateArray[0]['size'])) {
                        echo $first ? ', ' : '';
                        $first = true;
                        echo 'Size:' . $estateArray[0]['size'].' sqft';
                    }
                    if (isset($estateArray[0]['rate_per_unit'])) {
                        echo $first ? ', ' : '';
                        $first = true;
                        echo 'Rate/sqft :' . $estateArray[0]['rate_per_unit'];
                    }
                    /*
                if (isset($estateArray[0]['price'])) {
                    echo $first?',':'';
                    $first = true;
                }*/
                    echo ' ...';
                }
                echo '</p>';
                echo '</div>';
                echo '</div>';
            }
        } else{
            echo '<p>Sorry, no result found matching your criteria.<div class="initPad" >&nbsp;</div>';
        }
        }
        
    	if ('property' == strtolower($criteria['estate_type'])) {
            $basicCriteria = array('estate_id', 'state', 'district', 'city', 
            'is_featured', 'estate_type', 'name');
            $specCriteria = array('type_id', 'category_id', 'bhk', 
            'rate_per_unit', 'estate_for', 'size', 'sizeRangeFactor', 'price', 
            'priceRangeFactor');
            $basicParams = array_intersect_key($criteria, 
            array_flip($basicCriteria));
            $specParams = array_intersect_key($criteria, 
            array_flip($specCriteria));
            $projectMapper = new Reale_Model_Estate_PropertyMapper();
            $result = $projectMapper->searchByParams($basicParams, $specParams);
    	
        if (count($result)) {
        	$name = null;
        	if(isset($estateArray[0]['property_name'])){
        		$name = $estateArray[0]['property_name'];
        	}
        	else{
        		$name = "Name Not Provided";
        	}
        	
            foreach ($result as $estate_id => $estateArray) {
                echo '<div class="estateSet">';
                echo '<div class="estateInfo">';
                $url = $this->_helper->url('index', 'propertyprofile', 'default');
                $featured = $estateArray[0]['is_featured'] ? '*' : '';
                echo '<a class="resultTitle" href="' . $url ."?property_id=$estate_id".
                 '" target="_blank" > Property ' . $name . ', ' .
                 $estateArray[0]['district'] . ', ' . $estateArray[0]['state'] .
                 '</a>' . $featured;
                echo '<p class="resultBrief">';
                if (count($estateArray) > 1) {
                    echo 'There are ' . count($estateArray) . ' units in ' .
                     $estateArray[0]['project_name'] . ' matching your criteria.';
                } else {
                    $first = false;
                    if (isset($estateArray[0]['size'])) {
                        echo $first ? ', ' : '';
                        $first = true;
                        echo 'Size:' . $estateArray[0]['size'].' sqft';
                    }
                    if (isset($estateArray[0]['rate_per_unit'])) {
                        echo $first ? ', ' : '';
                        $first = true;
                        echo 'Rate/sqft :' . $estateArray[0]['rate_per_unit'];
                    }
                    /*
                if (isset($estateArray[0]['price'])) {
                    echo $first?',':'';
                    $first = true;
                }*/
                    echo ' ...';
                }
                echo '</p>';
                echo '</div>';
                echo '</div>';
            }
        } else{
            echo '<p>Sorry, no result found matching your criteria.<div class="initPad" >&nbsp;</div>';
        }
        }
        
        
    }
    /**
     * Search within projects
     */
    public function projectAction ()
    {}
    public function fillcityAction ()
    {
        $request = $this->getRequest();
        $format = $request->getParam('format', 'json');
        $district = $request->getParam('district');
        $mapper = new Reale_Model_Estate_SearchMapper();
        $result = $mapper->getCities($district);
        switch (strtolower($format)) {
            case 'json':
                $this->_helper->json($result);
                return;
            case 'jsonp':
                $callback = $request->getParam('callback');
                echo $callback . '(' . $this->_helper->json($result, false) . ')';
                return;
        }
    }
    public function filldistrictAction ()
    {
        $request = $this->getRequest();
        $format = $request->getParam('format', 'json');
        $state = $request->getParam('state');
        $mapper = new Reale_Model_Estate_SearchMapper();
        $result = $mapper->getDistricts($state);
        switch (strtolower($format)) {
            case 'json':
                $this->_helper->json($result);
                return;
            case 'jsonp':
                $callback = $request->getParam('callback');
                echo $callback . '(' . $this->_helper->json($result, false) . ')';
                return;
        }
    }
    public function fillstateAction ()
    {
        $request = $this->getRequest();
        $format = $request->getParam('format', 'json');
        $mapper = new Reale_Model_Estate_SearchMapper();
        $result = $mapper->getStates();
        switch (strtolower($format)) {
            case 'json':
                $this->_helper->json($result);
                return;
            case 'jsonp':
                $callback = $request->getParam('callback');
                echo $callback . '(' . $this->_helper->json($result, false) . ')';
                return;
        }
    }
}

